package edu.scau.yyl.ui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

import edu.scau.yyl.service.impl.UserServiceImpl;
import edu.scau.yyl.vo.User;

public class RegisterDialog extends JFrame implements ActionListener{
	
	private JLabel jlbuser=new JLabel("         用户名：");
	private JTextField jtfuser=new JTextField(18);
	private JLabel jlbpassword=new JLabel("        密    码：");
	private JPasswordField jtfpassword=new JPasswordField(18);
	private JLabel jlbpassword1=new JLabel("    确认密 码：");
	private JPasswordField jtfpassword1=new JPasswordField(18);
	private JButton jbtRegist=new JButton("注册");
	private JButton jbtCancel=new JButton("取消");
	private String password;
	private String password1;
	User user=new User();
	UserServiceImpl serviceImpl =null;

	public RegisterDialog(){
		
		jtfpassword.setEchoChar('*');
		jtfpassword1.setEchoChar('*');
		//界面排版设计
		JPanel p=new JPanel(null);
		JLabel b1=new JLabel(new ImageIcon("login.png"));
		JLabel b2=new JLabel(new ImageIcon("user.png"));
		JLabel b3=new JLabel(new ImageIcon("lock.png"));
		
		b1.setBounds(0, 0, 420, 60);
		b2.setBounds(98, 60, 20,20);
		jlbuser.setBounds(100,60,100,20);
		jtfuser.setBounds(180, 60, 120, 20);
		b3.setBounds(98, 80, 20, 20);
		jlbpassword.setBounds(100,80,100,20);
		jtfpassword.setBounds(180,80,120,20);
		jlbpassword1.setBounds(100,100,100,20);
		jtfpassword1.setBounds(180,100,120,20);
		jbtRegist.setBounds(130, 150, 60, 20);
		jbtCancel.setBounds(220, 150, 60, 20);
		
		p.add(b1);
		p.add(b2);
		p.add(b3);
		p.add(new JLabel(new ImageIcon("login.png")));
		p.add(jlbuser);
		p.add(jtfuser);
		p.add(jlbpassword);
		p.add(jtfpassword);
		p.add(jlbpassword1);
		p.add(jtfpassword1);
		p.add(jbtRegist);
		p.add(jbtCancel);
		this.add(p);
		//为按钮添加监听器
		
		jbtRegist.addActionListener(this);
		jbtCancel.addActionListener(this);
		
		this.setVisible(true);
		this.setSize(420, 260);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	
//		为文本框添加鼠标监听器
		jtfuser.addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e){//文本框中按下鼠标，将内容全部清除
				jtfuser.setText("");
			}
		});
		
    }
	
	@Override
	public void actionPerformed(ActionEvent e) {
		try {
			serviceImpl =new UserServiceImpl();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		if(e.getSource()==jbtRegist)
			password = new String(jtfpassword.getPassword());
			password1 = new String(jtfpassword1.getPassword());
			
			if(password.equals(password1)){
				 user.setUserID(user.getUserID());
				user.setUserName(jtfuser.getText());
			    user.setPassword(password);
			    try {
				if(serviceImpl.addUser(user)){
					new LoginDialog();
					this.setVisible(false);
		 			JOptionPane.showMessageDialog(null, "创建用户成功","提示",JOptionPane.INFORMATION_MESSAGE);
				}
			} catch (Exception e1) {
				
				e1.printStackTrace();
			}
			  
			}
			else{
				setVisible(false);
				JOptionPane.showMessageDialog(null, "创建用户失败","提示",JOptionPane.INFORMATION_MESSAGE);
			}    
			
		try {
			
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
	}if(e.getSource()==jbtCancel){
		this.setVisible(false);
	}

} 
	public static void main(String[] args){
		new RegisterDialog();
	}
}